package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper paramMapper;

    /**
     * 根据分类id查询分组
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid (Long cid){
        SpecGroup specGroup  =new SpecGroup();
        specGroup.setCid(cid);
        return  this.specGroupMapper.select(specGroup);
    }

    /**
     * 根据条件查询规格参数
     * @param gid
     * @param cid
     * @param generic
     * @param searching
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        return this.paramMapper.select(record);
    }

    /**
     * 新增组
     * @param group
     */
    public void addGroup(SpecGroup group){
        SpecGroup specGroup  =new SpecGroup();
        specGroup.setCid(group.getCid());
        specGroup.setName(group.getName());

        specGroupMapper.insert(specGroup);
    }

    /**
     * 修改组名称
     * @param specGroup
     */
    public  void editGroup(SpecGroup specGroup){
        SpecGroup group = new SpecGroup();
        group.setName(specGroup.getName());
        group.setId(specGroup.getId());
        this.specGroupMapper.updateByPrimaryKeySelective(group);
    }

    /**
     * 删除组数据
     * @param id
     */
    public  void deleteGroup(Long id){
        //先判断组里面有没有参数
        Example example =new Example(SpecParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("groupId",id);
        List<SpecParam> list = paramMapper.selectByExample(example);

        //若果没有，直接根据id删除组
        if(CollectionUtils.isEmpty(list)){
            specGroupMapper.deleteByPrimaryKey(id);
        }

        //如果有 ,先删除对应组id的参数,再删除组
          paramMapper.deleteByExample(example);
        specGroupMapper.deleteByPrimaryKey(id);
    }


    /**
     * 新增参数
     * @param param
     */
    public void addParam(SpecParam param){
        //    SpecParam specParam =new SpecParam();
        System.out.println(param);
        this.paramMapper.insert(param);
    }

    /**
     * 修改参数
     * @param param
     */
    public void editParam(SpecParam param){
        // System.out.println(param);
        this.paramMapper.updateByPrimaryKey(param);
    }

    /**
     * 根据id删除参数
     * @param id
     */
    public  void deleteParam(Long id){

        this.paramMapper.deleteByPrimaryKey(id);
    }
    /**
     *查询规格组，同时在规格组内的所有参数。
     * @Date  2020/1/1 11:50
     * @param cid
     * @return
     **/
    public List<SpecGroup> queryGroupWithParam(Long cid) {
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(group ->{
            //查询一组下的多个具体参数
            List<SpecParam> params = this.queryParams(group.getId(), null, null, null);
            group.setParams(params);
        });
        return groups;
    }
}
